﻿using System;
using System.Data;
using System.Windows.Forms;
using FoundationClass;
using FoundationClass.Entity;

namespace P2MARY_INTERNET.Controls
{
    public partial class PaymentForm : UserControl
    {
        public PaymentForm()
        {
            try
            {
                InitializeComponent();
                InitializeControl();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        private void InitializeControl()
        {
            pnlPaymentData.Enabled = grdPayment.SelectedCells.Count > 0;
            lblUsage.Text =
                lblStart.Text =
                lblPromotion.Text = lblEnd.Text = lblEmpId.Text = lblCompId.Text = lblAmount.Text = string.Empty;
        }

        public ServiceRecord ServicePayment;

        private void PaymentForm_Load(object sender, EventArgs e)
        {
            SetupGrid();
        }

        public User UserLogin;

        private void SetupGrid()
        {
            try
            {
                var dt = new DataTable();
                dt.Columns.Add("serviceid", typeof(string));
                dt.Columns.Add("compid", typeof(string));
                dt.Columns.Add("promotion", typeof(string));

                var services = MasterDataHandler.GetAllServiceRecordsClose();
                foreach (ServiceRecord service in services)
                {
                    dt.Rows.Add(service.Id, service.Computer.Id, service.Promotion.Name);
                }
                grdPayment.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"Payment Setupgrid: "+ex.Message);
            }
        }

        private void grdPayment_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                int id = int.Parse(grdPayment.SelectedRows[0].Cells["serviceid"].Value.ToString());
                var serviceRecord = MasterDataHandler.GetServiceRecord(id);
                serviceRecord.RecordStatus = 0;
                serviceRecord.Payment.PaidTime = DateTime.Now;
                serviceRecord.Payment.UpdatePayment();
                MasterDataHandler.UpdateSevicesRecord(serviceRecord);
                SetupGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"Payment Calcualte :" +ex.Message);
            }
        }

        private void grdPayment_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (grdPayment.SelectedRows.Count == 0 || grdPayment.SelectedRows[0].Index >= grdPayment.Rows.Count)
                    return;
                if (grdPayment.SelectedCells.Count == 0)
                    return;

                int id = int.Parse(grdPayment.SelectedRows[0].Cells["serviceid"].Value.ToString());
                var serviceRecord = MasterDataHandler.GetServiceRecord(id);
                lblCompId.Text = serviceRecord.Computer.Id.ToString();
                lblPromotion.Text = serviceRecord.Promotion.Name;

                lblStart.Text = serviceRecord.RecordStart.ToLongTimeString();
                lblEnd.Text = serviceRecord.RecordEnd.Value.ToLongTimeString();
                lblAmount.Text = serviceRecord.Payment.TotalAmount.ToString();

                lblUsage.Text = serviceRecord.RecordAmount;
                lblEmpId.Text = serviceRecord.Payment.Employee.Username;

                pnlPaymentData.Enabled = grdPayment.SelectedCells.Count > 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"Payment grid: "+ex.Message);
            }
        }
    }
}
